package com.nlx.notes.module.service.impl.flow;

import com.google.common.eventbus.Subscribe;
import com.nlx.notes.module.bean.bo.FlowMonthAmountStatisticsBO;
import com.nlx.notes.module.bean.dto.flow.FlowChangeDTO;
import com.nlx.notes.module.core.consts.GlobalConst;
import com.nlx.notes.module.core.util.DateUtils;
import com.nlx.notes.module.entity.FlowMonthStatistics;
import com.nlx.notes.module.service.IFlowMonthStatisticsService;
import com.nlx.notes.module.service.ITransactionsService;
import com.nlx.notes.module.service.flow.FlowStatisticsManageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class FlowMonthStatisticsManageServiceImpl implements FlowStatisticsManageService {

    private static final String STATISTICS_TYPE = "month";

    final IFlowMonthStatisticsService flowMonthStatisticsService;

    final ITransactionsService transactionsService;


    @Override
    public String getType() {
        return STATISTICS_TYPE;
    }

    @Subscribe
    @Override
    public void flowChange(FlowChangeDTO flowChangeDTO) {
        log.info("月统计被触发了、、、、");
        this.updateMonthAmount(flowChangeDTO.getUserId(),flowChangeDTO.getFlowNewDate(),flowChangeDTO.getBookId());
        this.updateMonthAmount(flowChangeDTO.getUserId(),flowChangeDTO.getFlowOldDate(),flowChangeDTO.getBookId());
    }

    private void updateMonthAmount(Long userId, LocalDate flowNewDate,Long bookId) {
        if(null == flowNewDate){
            return ;
        }
        String newMonthDate = DateUtils.formatMonth(flowNewDate);
        String newMonthFirstDate = DateUtils.formatFirstDayFortMonth(flowNewDate);
        String newMonthLastDate = DateUtils.formatLastDayForMonth(flowNewDate);
        FlowMonthAmountStatisticsBO flowMonthAmountStatisticsBO = transactionsService.sumByDate(newMonthFirstDate,newMonthLastDate, userId,bookId);
        FlowMonthStatistics monthStatistics = flowMonthStatisticsService.findByMonth(newMonthDate,bookId, userId);
        if(null == monthStatistics){
            monthStatistics = new FlowMonthStatistics();
            monthStatistics.setExpense(flowMonthAmountStatisticsBO.getExpenseAmount());
            monthStatistics.setIncome(flowMonthAmountStatisticsBO.getIncomeAmount());
            monthStatistics.setMonthTime(newMonthDate);
            monthStatistics.setYearDate(DateUtils.formatCurrentYear(flowNewDate));
            monthStatistics.setUserId(userId);
            monthStatistics.setBookId(bookId);
            monthStatistics.setDelFlag(GlobalConst.DEL_FLAG_NORMAL);
        }else{
            monthStatistics.setExpense(flowMonthAmountStatisticsBO.getExpenseAmount());
            monthStatistics.setIncome(flowMonthAmountStatisticsBO.getIncomeAmount());
        }
        flowMonthStatisticsService.saveOrUpdate(monthStatistics);
    }
}
